import time

from django.http import HttpRequest
from django.shortcuts import render,redirect

# Create your views here.
from Seller import models
from Seller.utils import encrypt, seller_decorator



def register(request:HttpRequest):
    if request.method == 'POST':
        username = request.POST.get("username")
        nickname = request.POST.get("nickname")
        password = request.POST.get("password")

        password = encrypt(password)

        models.Seller.objects.create(name = username,nickname=nickname,password=password)

        print("username:",username)
        print("nickname:", nickname)
        print("password:", password)

        return redirect('/Seller/login')


    return render(request,'Seller/register.html')


def login(request:HttpRequest):
    errorMsg=''
    if request.method == 'POST':
        username = request.POST.get("username")
        password = request.POST.get("password")
        password = encrypt(password)

        #数据库查询是否包含该用户或者密码是否正确
        result = models.Seller.objects.filter(name = username,password=password)

        if result:
            seller = result.last()
            request.session['username'] = username
            request.session['nickname'] = seller.nickname
            request.session['id'] = seller.id
            request.session['time'] = time.strftime('%Y-%M-%d %H:%M:%S',time.localtime(time.time()))
            return redirect('/Seller/index')
        else:
            errorMsg ='用户账户不存在或者密码错误'


    return render(request, 'Seller/login.html',{'error_msg':errorMsg })


@seller_decorator
def index(request:HttpRequest):
    return render(request, 'Seller/index.html')


def logout(request:HttpRequest):
    request.session.flush()
    return redirect('/Seller/login')

def type_list(request:HttpRequest):
    type_obj_list = models.Type.objects.all()
    return render(request, 'Seller/type_list.html',locals())

@seller_decorator
def type_add(request):
    if request.method == 'POST':
        type_name = request.POST.get("type_name")
        models.Type.objects.create(name=type_name)
    return render(request, 'Seller/type_add.html')

@seller_decorator
def type_change(request):
    if request.method == 'GET':
        id = request.GET.get('id')
        type_obj=models.Type.objects.get(id=id) # obj [obj]
        return render(request, 'Seller/type_change.html',{'type_obj':type_obj})
    else:
        id = request.POST.get('id')
        type_name = request.POST.get("type_name")
        models.Type.objects.filter(id=id).update(name=type_name)
    return redirect('/Seller/type_list')

@seller_decorator
def type_delete(request):
    if request.method == 'GET':
        id = request.GET.get('id')
        models.Type.objects.filter(id=id).delete()
    return redirect('/Seller/type_list')

def goods_add(request):
    seller_id = request.session.get('id')
    type_obj_list = models.Type.objects.all()
    if request.method == 'POST':
        # 1.商品编号
        goods_num = request.POST.get("goods_num")
        # 2.商品名称
        goods_name = request.POST.get("goods_name")
        # 3.商品原价
        goods_oprice = request.POST.get("goods_oprice")
        # 4.商品现价
        goods_xprice = request.POST.get("goods_xprice")
        # 5.商品库存
        goods_kucun = request.POST.get("goods_kucun")
        # 6. 商品销量
        goods_sales = request.POST.get("goods_saLes")
        # 7.商品描述
        goods_description = request.POST.get("goods_description")
        # 8.商品缩略图
        userfiles = request.FILES.getlist('userfiles')
        # 9. 商品类型id
        goods_type = request.POST.get("goods_type")
        # 10.商品详情
        goods_content = request.POST.get("goods_content")

        goodsObject = models.Goods(
            goods_num=goods_num,
            goods_name=goods_name,
            goods_oprice=goods_oprice,
            goods_cprice=goods_xprice,
            goods_kucun=goods_kucun,
            goods_description=goods_description,
            goods_detail=goods_content,
            goods_sales=goods_sales,
            types=models.Type.objects.get(id=goods_type),
            seller=models.Seller.objects.get(id=seller_id)
        )

        goodsObject.save()

    return render(request,'Seller/goods_add.html',locals())

def goods_list(request):
    return render(request,'Seller/good_list.html',locals())